package com.mes.system.service.impl;

import java.util.List;

import cn.hutool.core.collection.CollectionUtil;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.github.pagehelper.PageInfo;
import com.mes.common.core.domain.BaseEntity;
import com.mes.common.core.service.BaseServiceImpl;
import com.mes.system.mapper.SysAutoCodeRuleMapper;
import com.mes.system.domain.SysAutoCodeRule;
import com.mes.system.service.SysAutoCodeRuleService;
import org.springframework.transaction.annotation.Transactional;
import com.mes.system.mapper.SysAutoCodePartMapper;
import com.mes.system.domain.SysAutoCodePart;
import org.springframework.beans.factory.annotation.Autowired;
import com.mes.common.exception.CustomException;

/**
 * 编码生成规则表Service业务层处理
 * @author hwg
 * @email hwg
 * @date 2023-04-22
 */
@Service
@Transactional(readOnly = true)
public class SysAutoCodeRuleServiceImpl extends BaseServiceImpl<SysAutoCodeRuleMapper, SysAutoCodeRule> implements SysAutoCodeRuleService {

    private static final Logger log = LoggerFactory.getLogger(SysAutoCodeRuleServiceImpl.class);

    @Autowired(required = false)
    private SysAutoCodePartMapper sysAutoCodePartMapper;

    /**
     * 获取单条数据
     * @param sysAutoCodeRule 编码生成规则表
     * @return 编码生成规则表
     */
    @Override
    public SysAutoCodeRule get(SysAutoCodeRule sysAutoCodeRule) {
        SysAutoCodeRule dto = super.get(sysAutoCodeRule);
        return dto;
    }

    /**
     * 获取单条数据
     * @param id 编码生成规则表id
     * @return 编码生成规则表
     */
    @Override
    public SysAutoCodeRule get(String id) {
        SysAutoCodeRule dto = super.get(id);
        return dto;
    }

    /**
     * 查询编码生成规则表列表
     * @param sysAutoCodeRule 编码生成规则表
     * @return 编码生成规则表
     */
    @Override
    public List<SysAutoCodeRule> findList(SysAutoCodeRule sysAutoCodeRule) {
		List<SysAutoCodeRule> sysAutoCodeRuleList = super.findList(sysAutoCodeRule);
        return sysAutoCodeRuleList;
    }

    /**
     * 分页查询编码生成规则表列表
     * @param sysAutoCodeRule 编码生成规则表
     * @return 编码生成规则表
     */
    @Override
    public PageInfo<SysAutoCodeRule> findPage(SysAutoCodeRule sysAutoCodeRule) {
		PageInfo<SysAutoCodeRule> page = super.findPage(sysAutoCodeRule);
        return page;
    }

    /**
     * 保存编码生成规则表
     * @param sysAutoCodeRule
     * @return 结果
     */
    @Override
    public boolean save(SysAutoCodeRule sysAutoCodeRule) {
        return super.save(sysAutoCodeRule);
    }

    /**
     * 删除编码生成规则表信息
     * @param sysAutoCodeRule
     * @return 结果
     */
    @Override
    public boolean remove(SysAutoCodeRule sysAutoCodeRule) {
        return super.remove(sysAutoCodeRule);
    }

    /**
     * 批量删除编码生成规则表
     * @param ids 需要删除的编码生成规则表ID
     * @return 结果
     */
    @Transactional(readOnly = false)
    @Override
    public int deleteSysAutoCodeRuleByIds(String[] ids) {
        for (String id : ids)
        {
            SysAutoCodePart sysAutoCodePart = new SysAutoCodePart();
            sysAutoCodePart.setRuleId(id);
            List<SysAutoCodePart> sysAutoCodePartList = sysAutoCodePartMapper.findList(sysAutoCodePart);
            if (sysAutoCodePartList.size() > 0)
            {
                throw new CustomException("子表存在数据,不能删除");
            }
        }
        return mapper.deleteSysAutoCodeRuleByIds(ids, BaseEntity.DEL_FLAG_DELETE);
    }

    @Override
    public boolean checkRuleCodeUnique(SysAutoCodeRule sysAutoCodeRule) {
        SysAutoCodeRule rule = mapper.checkRuleCodeUnique(sysAutoCodeRule.getRuleCode());
        if(rule!=null && !rule.getId().equals(sysAutoCodeRule.getId())) {
            return false;
        }
        return true;
    }

    @Override
    public boolean checkRuleNameUnique(SysAutoCodeRule sysAutoCodeRule) {
        SysAutoCodeRule rule = mapper.checkRuleNameUnique(sysAutoCodeRule.getRuleName());
        if(rule!=null && !rule.getId().equals(sysAutoCodeRule.getId())) {
            return false;
        }
        return true;
    }

    @Override
    public SysAutoCodeRule getOne(String ruleCode) {
        SysAutoCodeRule param = new SysAutoCodeRule();
        param.setRuleCode(ruleCode);
        List<SysAutoCodeRule> rules = mapper.selectSysAutoCodeResultList(param);
        if(CollectionUtil.isNotEmpty(rules)){
            return rules.get(0);
        }
        return null;
    }

}
